package com.gs.cd.api;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Getter
@Setter
@ToString
public class ApiResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;


    @JsonProperty("page_no")
    private Long pageNo;
    @JsonProperty("page_size")
    private Long pageSize;
    @JsonProperty("total")
    private Long total;
    @JsonProperty("records")
    private List<T> records;
    @JsonProperty("extra_information")
    private Object extra;

    private ApiResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ApiResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ApiResult(Integer code, String msg, Long pageNo, Long pageSize, Long total, List<T> records, Object extra) {
        this.code = code;
        this.msg = msg;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
        this.records = records;
        this.extra = extra;
    }

    private ApiResult() {
    }

    public static ApiResult success() {
        return new ApiResult(0, "操作成功");
    }

    public static ApiResult success(String msg) {
        return new ApiResult(0, msg);
    }

    public static <T> ApiResult<T> success(String msg, T t) {
        return new ApiResult<T>(0, msg, t);
    }

    public static <T> ApiResult<T> success(Page<T> iPage) {
        return new ApiResult<T>(0, "操作成功", iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getRecords(), null);
    }


    public static <T> ApiResult<T> success(T t) {
        return new ApiResult<T>(0, "操作成功", t);
    }

    public static ApiResult error() {
        return new ApiResult(500, "操作失败");
    }

    public static ApiResult error(String msg) {
        return new ApiResult(500, msg);
    }

    public static <T> ApiResult<T> error(String msg, T t) {
        return new ApiResult<T>(500, msg, t);
    }

    public static <T> ApiResult<T> error(ApiResultType apiResultType, String msg, T t) {
        return new ApiResult<T>(apiResultType.getValue(), msg, t);
    }

    public static <T> ApiResult<T> error(T t) {
        return new ApiResult<T>(500, "操作失败", t);
    }

    public static <T> ApiResult<T> error(ApiResultType apiResultType, T t) {
        return new ApiResult<T>(apiResultType.getValue(), apiResultType.getDes(), t);
    }

    public static <T> ApiResult success(long current, long size, long total, List<T> records) {
        return new ApiResult(0, "操作成功", current, size, total, records, null);
    }

    public boolean suc() {
        return code == 0;
    }
}